package com.gykfbxxh.imovie.base;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;

import com.app.utils.util.LogUtils;
import com.gykfbxxh.imovie.custom.view.dialog.CustomDialog;
import com.gykfbxxh.imovie.manager.AppManager;

import java.util.ArrayList;

/**
 * Created by xuxinghai on 2017/4/7.
 * <p>
 * <p>
 * 获取运行时权限的activity,
 */

public class PermissionRequestActivity extends AppCompatActivity {


    private static PermissionListener mlistener;
    private static final int REQUEST_PERMISSION_CODE = 59;


    /**
     * 获取权限的回调
     */
    public interface PermissionListener {
        void onGranted();

        void onDenied(ArrayList<String> deniedList);
    }


    /**
     * 检查权限
     *
     * @param perms
     * @return
     */
    public static boolean hasPermissions(String... perms) {
        // 小于6.0则不必再申请权限,直接返回
        LogUtils.e("gzq", "build_version = "+Build.VERSION.SDK_INT);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return true;
        }

        Activity topActivity = AppManager.getAppManager().getLastActivity();
        if (topActivity == null) return false;


        for (String permission : perms) {
            if (ContextCompat.checkSelfPermission(topActivity, permission) != PackageManager.PERMISSION_GRANTED) {//有权限没有被授权
                return false;
            }
        }
        return true;
    }


    /**
     * 请求权限
     *
     * @param permissions
     * @param listener
     */
    public static void requestRuntimePermission(String[] permissions, PermissionListener listener) {
        mlistener = listener;
//        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M){//如果当前版本小于6.0,则不必请求运行时权限
//            if(mlistener != null){
//                mlistener.onGranted();
//            }
//        }else{
        Activity topActivity = AppManager.getAppManager().getLastActivity();
        if (topActivity == null) return;


        ArrayList<String> permissionList = new ArrayList<>();
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(topActivity, permission) != PackageManager.PERMISSION_GRANTED) {//有权限没有被授权
                permissionList.add(permission);
            }
        }
        if (!permissionList.isEmpty()) {//存在没有授权的权限
            //请求授权
            ActivityCompat.requestPermissions(topActivity, permissionList.toArray(new String[permissionList.size()]), REQUEST_PERMISSION_CODE);

        } else {
            if (mlistener != null) {
                listener.onGranted();
            }
        }
//        }


    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        switch (requestCode) {
            case REQUEST_PERMISSION_CODE:
                if (grantResults.length > 0) {
                    //被拒绝的权限
                    ArrayList<String> deniedPermissions = new ArrayList<>();
                    //不在提示 的权限
                    ArrayList<String> dontAskAgain = new ArrayList<>();
                    for (int i = 0; i < grantResults.length; i++) {
                        int grantResult = grantResults[i];
                        String permission = permissions[i];
                        if (grantResult != PackageManager.PERMISSION_GRANTED) {//当前权限被拒绝了
                            deniedPermissions.add(permission);
                            //如果有不再被提醒的点击
                            if (!ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[i])) {
                                dontAskAgain.add(permissions[i]);
                            }
                        }

                    }

                    if (deniedPermissions.isEmpty()) {//所有权限都被授权
                        if (mlistener != null) {
                            mlistener.onGranted();
                        }
                    } else {//有某个权限被拒绝了,并且 有权限提示不在被提醒
                        if (mlistener != null && !dontAskAgain.isEmpty()) {
                            mlistener.onDenied(dontAskAgain);
                            LogUtils.e("gzq", "权限被拒绝");
                            Activity topActivity = AppManager.getAppManager().getLastActivity();
                            if (topActivity != null) {
                                CustomDialog.getInstance().showPermissionDialog(topActivity,false,dontAskAgain.toArray(new String[dontAskAgain.size()]));
                            }

                        }
                    }
                }

                break;

            default:
                break;
        }
    }




}
